package com.itshizhan.itjidancideveloper.response

import com.fasterxml.jackson.annotation.JsonInclude
import java.util.*

@JsonInclude(JsonInclude.Include.NON_NULL)
class Response<T>(
    val code: Int,
    val msg: String,
    val data: T?,
    val timestamp: Long,
){

  constructor(code: Int, msg: String) : this(
      code = code,
      msg = msg,
      data = null,
      timestamp = timestamp()
  )


  companion object {

    fun <T> success(): Response<T> {
      return Response.code(CodeMsgEnum.SUCCESS)
    }

    fun <T> success(data: T): Response<T> {
      return Response(CodeMsgEnum.SUCCESS.code, CodeMsgEnum.SUCCESS.msg, data, timestamp())
    }

    fun <T> fail(): Response<T> {
      return Response.code(CodeMsgEnum.FAIL)
    }

    fun <T> fail(msg: String): Response<T> {
      return Response(CodeMsgEnum.FAIL.code, msg, null, timestamp())
    }

    fun <T> fail(code: Int, msg: String): Response<T> {
      return Response(code, msg, null, timestamp())
    }

    fun <T> code(msgCode: CodeMsg): Response<T> {
      return Response(msgCode.code, msgCode.msg)
    }

    private fun timestamp(): Long {
      return Calendar.getInstance().timeInMillis
    }
  }

  override fun toString(): String {
    return "Response(code=$code, msg='$msg', data=$data, timestamp=$timestamp)"
  }


}